package com.seaboxdata.interfaceJob;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seaboxdata.entity.Eval;
import com.seaboxdata.service.EvalService;
import com.seaboxdata.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

@Component
@Configuration
@Slf4j
public class EvalJob {

    @Value("${hcp.eval.url}")
    private String url;

    @Value("${hcp.eval.appKey}")
    private String appKey;

    @Value("${hcp.eval.appToken}")
    private String appToken;

    @Value("${hcp.eval.pageSize}")
    private String pageSize;

    @Value("${hcp.eval.flag}")
    private String flag;

    @Value("${interface.isTest}")
    private String isTest;

    @Autowired
    private EvalService evalService;


    @Scheduled(cron = "${hcp.eval.interval}")//每隔5分钟执行一次
    public void doCollect() {
        log.info("collect eval begin");
        //如果是测试,直接退出
        if("true".equals(isTest)){
            return;
        }
        String format = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String startDate = sdf.format(DateUtils.addDays(new Date(), -1)) + " 00:00:00";
        String endDate = sdf.format(new Date()) + " 00:00:00";
        handler(1, Integer.valueOf(pageSize), appKey, startDate, endDate, flag);
        log.info("collect eval end");

        /*
        log.info("collect eval begin");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date beginStartDate = sdf.parse("2019-09-01");
        Date todayDate =  new Date();
        String endDate = null;
        while(beginStartDate.compareTo(todayDate) < 0){
            endDate = sdf.format(DateUtils.addDays(beginStartDate,1));
            handler(1, Integer.valueOf(pageSize), appKey, sdf.format(beginStartDate) + " 00:00:00", endDate + " 00:00:00", flag);
            beginStartDate = DateUtils.addDays(beginStartDate,1);
        }
        */
    }


    public void handler(int page, int pageSize, String appKey, String startDate, String endDate, String flag) {
        Map<String, Object> map = new HashMap<>();
        map.put("appCode", appKey);
        map.put("page", page);
        map.put("size", pageSize);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("flag", flag);
        String response = null;
        List<Eval> evals = new ArrayList<>();
        try {
            response = HttpUtil.doPost(url, map, appKey, appToken);
            if (response != null) {
                JSONObject jsonObject = JSON.parseObject(response);
                int state = (int) jsonObject.get("state");
                String message = (String) jsonObject.get("message");
                if (state == 0) {
                    log.error("get eval failed,failed message:" + message);
                    return;
                }
                JSONObject rows = (JSONObject) jsonObject.get("row");
                JSONArray data = (JSONArray) rows.get("list");

                Iterator<Object> iterator = data.iterator();
                while (iterator.hasNext()) {
                    JSONObject result = (JSONObject) iterator.next();
                    Eval eval = result.toJavaObject(Eval.class);
                    evals.add(eval);
                }
                //入库
                if (!CollectionUtils.isEmpty(evals)) {
                    evalService.saveBatch(evals);
                }

                //再次调用接口
                if ((boolean) rows.get("hasNextPage")) {
                    handler(page + 1, pageSize, appKey, startDate, endDate, flag);
                }
                ;
            }
        } catch (Exception e) {
            log.error("collect eval error");
            e.printStackTrace();
        }
    }
}
